package cn.hfuu.spark;

import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.sql.SparkSession;
import scala.Tuple2;

import java.util.Arrays;
import java.util.Map;

/**
 * @author oldliu
 * @since 1.0
 */
public class WordCount3 {
    public static void main(String[] args) {
        // 创建SparkSession
        SparkSession spark = SparkSession.builder()
                .appName("SparkApp")
                .master("local")
                .getOrCreate();

        // 创建JavaSparkContext
        JavaSparkContext jsc = new JavaSparkContext(spark.sparkContext());

        JavaRDD<String> lines = jsc.textFile("hdfs://hadoopmaster:9000/heda2022");

        //?String n = lines.max((x, y) -> x.length());
        //?System.out.println("最大的是 is :" + n);

        JavaPairRDD<String, Integer> words =
                lines.flatMap(x -> Arrays.asList(x.split(" ")).iterator()).filter(x -> x.length() >= 3).mapToPair(word -> new Tuple2<>(word, 1)).reduceByKey((a, b) -> a + b);
 
        Tuple2<String, Integer> max = words.max((x, y) -> y._2 - x._2);
        System.out.println("出现次数最多的是：" + max._1 + ",次数是：" + max._2);
    }
}
